package com.java.yz.day10.ketang;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/*
    存储自定义对象并遍历
    Student(name,age)
 */
class Student{
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class CollectionText2 {
    public static void main(String[] args) {

        //创建Collection对象
        Collection c1 = new ArrayList();

        //创建学生对象
        Student s1 = new Student("张翰", 19);
        Student s2 = new Student("顾文泽", 29);
        Student s3 = new Student("杨镇", 23);

        //向集合中添加元素
        c1.add(s1);
        c1.add(s2);
        c1.add(s3);

        //遍历元素
        Iterator iterator = c1.iterator();
        while (iterator.hasNext()){
            Object next = iterator.next();
            Student student=(Student) next;
            System.out.println(student.getName()+"-- "+student.getAge());
        }
    }
}
